<?php
namespace app\widget\controller;

use tp5_lib\common\controller\BaseController;
use tp5_lib\menu\logic\Menu;
use tp5_lib\access\logic\RoleLogic;

/**
 * 页面示例
 */
class PageController extends BaseController
{
    /**
     * @var Menu $nav 导航栏菜单
     * @var Menu $menu 主菜单
     */
    protected $nav=null;
    protected $menu=null;
    /** 初始化导航栏和菜单 */
    protected function init()
    {
        $this->nav=(new Menu())->setMenu([
            "title"=>"导航栏",
            "sub"=>[
                "index"=>[
                    "title"=>"主页",
                    "target"=>"inline",
                    "type"=>"url_path",
                    "link"=>"/../index.php/",
                    "target"=>"",
                ],
                "widget"=>[
                    "title"=>"控件",
                    "target"=>"inline",
                    "type"=>"url_path",
                    "link"=>"/widget/test",
                    "target"=>"",
                ],
            ],
        ]);
        $this->menu=(new Menu())->setName("widget");
    }
    /** 设置虚拟用户-为了方便用户查看某个角色的功能，只加载对应角色的菜单 */
    protected function startAsRole()
    {
        $role=RoleLogic::instance();
        $role_as="";
        if($_role_as = input("role_as")){
            if($_role_as==="merge"){
                session("role_as",null);
            }
            else{
                session("role_as",$_role_as);
                $role_as=$_role_as;
            }
        }
        elseif($_role_as = session("role_as")){
            $role_as=$_role_as;
        }
        if($role_as){
            $role->setRoleNames($role_as,false);
        }
        $this->role_as=$role_as;
    }
    /** 取消虚拟用户 */
    protected function endAsRole()
    {
        $role=RoleLogic::instance();
        $role_as = isset($this->role_as) ? $this->role_as : null;
        if($role_as){
            $role->setRoleNames(null,false);
        }
        $roles=$role->getRoles();
        $roles["merge"]=[
            "name"=>"复合模式",
        ];
        if($role_as && isset($roles[$role_as])){
            $roles[$role_as]["active"]=true;
        }
        else{
            $roles["merge"]["active"]=true;
        }
        foreach($roles as $key => $role){
            $role["url"]=url() ."?". http_build_query(["role_as"=>$key]);
            $roles[$key]=$role;
        }
        return $roles;
    }
    /** 整体页面，一般是文章、商品的列表、详情页之类的页面 */
    public function index()
    {
        $this->init();
        $this->nav->setCurrent("widget");

        $this->assign([
            "title"=>"测试页面",
            "keywords"=>"测试,页面",
            "description"=>"内容介绍",
            "content"=>"test",
        ]);
        
        //在文章、商品的频道、列表页中，一般是显示一个或多个列表。这里没有表示，直接用菜单代替
        $this->assign("menu",$this->menu->getMenu());
        $this->assign("bread_crumb",$this->menu->getBreadCrumb());
        $this->assign("nav",$this->nav->getMenu());
        return $this->fetch();
    }
    /** 框架结构页面，一般是管理后台之类的页面 */
    public function frame()
    {
        $user=$this->checkLogin();
        $this->init();

        //绑定页面位置和内容
        if($current = input("path")) $this->menu->setCurrent($current);
        $this->nav->setCurrent("widget");

        $this->assign([
            "title"=>"测试页面",
            "keywords"=>"测试,页面",
            "description"=>"内容介绍",
        ]);

        $this->startAsRole();
        $this->assign("menu",$this->menu->getMenu());
        $this->assign("bread_crumb",$this->menu->getBreadCrumb());
        $this->assign("current_href",$this->menu->getCurrentHref());
        
        $roles=$this->endAsRole();
        $this->assign("roles",$roles);

        $this->assign("nav",$this->nav->getMenu());
        return $this->fetch();
    }
    /** 内联框架页面 */
    public function iframe()
    {
        $user=$this->checkLogin();
        config("app_trace",false);
        return $this->fetch();
    }
}
